﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using TinyERP.Framework.Controls;
namespace TinyERP.Framework.Controls
{
    public partial class Paging : UserControl, ILocalizable
    {
        public event PageChangedHandler OnPageChanged;
        public Paging()
        {
            InitializeComponent();
            WireEvents();
        }
        private int pageSize = 2;
        private int currentPage = 1;
        private long totalRecord;
        private int totalPage;

        private ComboBox cbPage;
        private KLabel lblPage;
        private KLabel lblSum;
        private FlowLayoutPanel flowLayoutPanel;
        private FirstButton firstButton;
        private PreviousButton previousButton;
        private NextButton nextButton;
        private LastButton lastButton;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Paging));
            this.firstButton = new TinyERP.Framework.Controls.FirstButton();
            this.previousButton = new TinyERP.Framework.Controls.PreviousButton();
            this.nextButton = new TinyERP.Framework.Controls.NextButton();
            this.lastButton = new TinyERP.Framework.Controls.LastButton();
            this.cbPage = new System.Windows.Forms.ComboBox();
            this.lblPage = new KLabel();
            this.lblSum = new KLabel();
            this.flowLayoutPanel = new System.Windows.Forms.FlowLayoutPanel();
            this.flowLayoutPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // firstButton
            // 
            this.firstButton.AutoSize = true;
            this.firstButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.firstButton.Location = new System.Drawing.Point(127, 3);
            this.firstButton.Name = "firstButton";
            this.firstButton.ResourceKey = "First";
            this.firstButton.Size = new System.Drawing.Size(48, 23);
            this.firstButton.TabIndex = 1;
            // 
            // previousButton
            // 
            this.previousButton.AutoSize = true;
            this.previousButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.previousButton.Location = new System.Drawing.Point(181, 3);
            this.previousButton.Name = "previousButton";
            this.previousButton.Size = new System.Drawing.Size(69, 23);
            this.previousButton.TabIndex = 2;
            // 
            // nextButton
            // 
            this.nextButton.AutoSize = true;
            this.nextButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.nextButton.Location = new System.Drawing.Point(370, 3);
            this.nextButton.Name = "nextButton";
            this.nextButton.Size = new System.Drawing.Size(51, 23);
            this.nextButton.TabIndex = 3;
            // 
            // lastButton
            // 
            this.lastButton.AutoSize = true;
            this.lastButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.lastButton.Location = new System.Drawing.Point(427, 3);
            this.lastButton.Name = "lastButton";
            this.lastButton.Size = new System.Drawing.Size(47, 23);
            this.lastButton.TabIndex = 4;

            // 
            // cbPage
            // 
            this.cbPage.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbPage.FormattingEnabled = true;
            this.cbPage.Location = new System.Drawing.Point(294, 3);
            this.cbPage.Name = "cbPage";
            this.cbPage.Size = new System.Drawing.Size(70, 21);
            this.cbPage.TabIndex = 5;
            // 
            // lblPage
            // 
            this.lblPage.AutoSize = true;
            this.lblPage.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lblPage.Location = new System.Drawing.Point(256, 0);
            this.lblPage.Name = "lblPage";
            this.lblPage.Size = new System.Drawing.Size(32, 29);
            this.lblPage.TabIndex = 4;
            this.lblPage.Text = AppResourceManager.GetInstance().GetString("Page");
            this.lblPage.ResourceKey = "Page";
            
            // 
            // lblSum
            // 
            this.lblSum.Dock = System.Windows.Forms.DockStyle.Left;
            this.lblSum.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblSum.Location = new System.Drawing.Point(0, 0);
            this.lblSum.Name = "lblSum";
            this.lblSum.Size = new System.Drawing.Size(158, 30);
            this.lblSum.TabIndex = 6;
            this.lblSum.Text = AppResourceManager.GetInstance().GetString("TotalRecordSummary");
            
            // 
            // flowLayoutPanel
            // 
            this.flowLayoutPanel.Controls.Add(this.lastButton);
            this.flowLayoutPanel.Controls.Add(this.nextButton);
            this.flowLayoutPanel.Controls.Add(this.cbPage);
            this.flowLayoutPanel.Controls.Add(this.lblPage);
            this.flowLayoutPanel.Controls.Add(this.previousButton);
            this.flowLayoutPanel.Controls.Add(this.firstButton);
            this.flowLayoutPanel.Dock = System.Windows.Forms.DockStyle.Right;
            this.flowLayoutPanel.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
            this.flowLayoutPanel.Location = new System.Drawing.Point(265, 0);
            this.flowLayoutPanel.Name = "flowLayoutPanel";
            this.flowLayoutPanel.Size = new System.Drawing.Size(477, 30);
            this.flowLayoutPanel.TabIndex = 7;
            // 
            // Paging
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.Controls.Add(this.flowLayoutPanel);
            this.Controls.Add(this.lblSum);
            this.Name = "Paging";
            this.Size = new System.Drawing.Size(742, 30);
            this.flowLayoutPanel.ResumeLayout(false);
            this.flowLayoutPanel.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        public int CurrentPage
        {
            get { return currentPage - 1; }
            set 
            { 
                currentPage = value + 1; 
            }
        }

        public long TotalRecord
        {
            get { return totalRecord; }
            set
            {
                UpdateTotalRecord(value);
            }
        }
        public void UpdateTotalRecord(long totalRecord)
        {
            this.totalRecord = totalRecord;
            ResetPagingStatus();
            totalPage = CalculateNumOfPage(totalRecord);
            BuildPageControl(totalPage);
            PrintSummaryLabel();
            UpdateNavigationButtons();
        }
        private void UpdateNavigationButtons()
        {
            previousButton.Enabled = (currentPage != 1);
            firstButton.Enabled = (currentPage != 1);
            lastButton.Enabled = (currentPage != totalPage);
            nextButton.Enabled = (currentPage != totalPage);
        }
        private void PrintSummaryLabel()
        {
            lblSum.Text = string.Format(AppResourceManager.GetInstance().GetString("TotalRecordSummary"), totalRecord);
        }
        private void BuildPageControl(int totalPage)
        {
            this.cbPage.Items.Clear();
            for (int i = 1; i <= totalPage; i++)
            {
                this.cbPage.Items.Add(i);
            }
            this.cbPage.SelectedItem = this.currentPage;
            cbPage.Enabled = cbPage.Items.Count != 1;
        }
        private void ResetPagingStatus()
        {
            lblSum.Text = "";
            this.previousButton.Enabled = false;
            this.nextButton.Enabled = false;
            this.lastButton.Enabled = false;
            this.firstButton.Enabled = false;
        }
        private int CalculateNumOfPage(long totalRecord)
        {
            int numOfPage = (int)(totalRecord / pageSize);
            if (totalRecord % pageSize != 0)
            {
                numOfPage++;
            }
            if (numOfPage == 0)
            {
                numOfPage = 1;
            }
            return numOfPage;
        }
        public void WireEvents()
        {
            cbPage.SelectedValueChanged += new EventHandler(OnPageComboSelectedValueChanged);
            previousButton.Click += new EventHandler(OnPreviousButtonClick);
            nextButton.Click += new EventHandler(OnNextButtonClick);
            firstButton.Click += new EventHandler(OnFirstButtonClick);
            lastButton.Click += new EventHandler(OnLastButtonClick);
        }

        private void NotifyPageChanged()
        {
            if (OnPageChanged != null)
            {
                PagingArg arg = new PagingArg();
                arg.CurrentPage = currentPage - 1;
                OnPageChanged(this, arg);
            }
        }
        private void OnPreviousButtonClick(object sender, EventArgs e)
        {
            if (currentPage != 1)
            {
                currentPage -= 1;
                this.cbPage.SelectedItem = currentPage;
                NotifyPageChanged();
            }
            UpdateNavigationButtons();
        }
        private void OnNextButtonClick(object sender, EventArgs e)
        {
            if (currentPage != totalPage)
            {
                currentPage += 1;
                this.cbPage.SelectedItem = currentPage;
                NotifyPageChanged();
            }
            UpdateNavigationButtons();
        }
        private void OnFirstButtonClick(object sender, EventArgs e)
        {
            if (currentPage != 1)
            {
                currentPage = 1;
                this.cbPage.SelectedItem = currentPage;
                NotifyPageChanged();
            }
            UpdateNavigationButtons();
        }

        private void OnLastButtonClick(object sender, EventArgs e)
        {
            if (currentPage != totalPage)
            {
                currentPage = totalPage;
                this.cbPage.SelectedItem = currentPage;
                NotifyPageChanged();
            }
            UpdateNavigationButtons();
        }

        private void OnPageComboSelectedValueChanged(object sender, EventArgs e)
        {
            if (cbPage.SelectedItem != null)
            {
                int pageValue = int.Parse(cbPage.SelectedItem.ToString());
                if (currentPage != pageValue)
                {
                    currentPage = pageValue;
                    NotifyPageChanged();
                    UpdateNavigationButtons();
                }
            }

        }


        #region ILocalizable Members

        public void UpdateLanguage()
        {
            this.PrintSummaryLabel();
        }

        #endregion
    }
    public class PagingArg
    {
        public int CurrentPage { get; set; }
    }
    public delegate void PageChangedHandler(object sender, PagingArg arg);
}
